import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { Tools } from "../common/Tools";
import { EventManager } from "../manager/EventManager";
import { LevelManager } from "../manager/LevelManager";
import { ResManager1 } from "../manager/ResManager1";
import { UIManager } from "../manager/UIManager";
import { LvInfo } from "../model/OtherModel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelItem extends cc.Component {

    @property(cc.Sprite)
    lvSpr: cc.Sprite = null;
    @property(cc.Label)
    title: cc.Label = null;
    @property(cc.Node)
    newSpr: cc.Node = null;
    @property(cc.Node)
    hotSpr: cc.Node = null;
    @property(cc.Node)
    lockSpr: cc.Node = null;
    @property(cc.Node)
    addLiveLv1: cc.Node = null;
    @property(cc.Node)
    addLiveLv2: cc.Node = null;
    @property(cc.Node)
    YNPass: cc.Node = null;
    unlocked: boolean = false;
    data: LvInfo;

    async initLevelItem(data: LvInfo) {
        this.data = data;

        this.updateLvSpr();
        this.updateTitle();
        this.updateNewSpr();
        this.updateHotSpr();
        this.updateLockState();
        this.updateAddLiveState();
        this.ceshiLv()
    }
    async updateLvSpr() {
        if (this.data) {
            let url = 'game_' + this.data.type + '_' + this.data.lv;
            this.lvSpr.spriteFrame = await ResManager1.ins.loadRes(url, cc.SpriteFrame, BUNDLE_TYPE_ENUM.LEVEL_SPR);
        } else {
            this.lvSpr.spriteFrame = null;
        }
    }
    updateTitle() {
        this.title.string = this.data ? this.data.name : "敬请期待";
    }
    updateNewSpr() {
        this.newSpr.active = this.data ? this.data.isNew : false;
    }
    updateHotSpr() {
        this.hotSpr.active = this.data ? this.data.isHot : false;
    }
    //通过 未通过
    ceshiLv() {
        this.YNPass.active = true;
        if (!this.lockSpr.active) {
            // this.YNPass.active = true;
            this.YNPass.children[1].active = false;
            this.YNPass.children[0].active = true;
        } else {
            // this.YNPass.active = false;
        }
        if (!this.data) {
            return;
        }
        let passLv = Tools.getStorage('PassLv_1', true);
        if (passLv == null) {
            this.YNPass.children[0].active = true;
        } else {
            if (passLv instanceof String) {
                Tools.setStorage('PassLv_1', [], true);
            }
            for (let i = 0; i < passLv.length; i++) {
                if (passLv[i] === (this.data.type + '_' + this.data.lv)) {
                    this.YNPass.children[1].active = true;
                    this.YNPass.children[0].active = false;
                }
            }
        }
    }
    updateAddLiveState() {
        this.addLiveLv1.active = false;
        this.addLiveLv2.active = false;
        let isLiveLevelActive = Tools.getStorage('setLiveLvActive');
        if (isLiveLevelActive && isLiveLevelActive != "false") {
            this.addLiveLv1.active = true;
            let liveLvInfos = LevelManager.ins.getLiveLvInfos();
            if (liveLvInfos.length > 0) {
                for (let item of liveLvInfos) {
                    if (item.name == this.data.name) {
                        this.addLiveLv2.active = true;
                        this.addLiveLv1.active = false;
                    }
                }
            }
        }
    }

    updateLockState() {
        if (this.data) {
            this.unlocked = LevelManager.ins.isLevelUnlock(this.data.type, this.data.lv);
            // cc.log(this.data.type + "_" + this.data.lv, this.unlocked);
        } else {
            this.unlocked = false;
        }

        this.lockSpr.active = !this.unlocked;
        this.ceshiLv()
        // if (this.unlocked) {
        //     Tools.setSpriteState(this.node, 0, true);
        //     Tools.setSpriteState(this.node.getChildByName("newSpr"), 0, true);
        //     Tools.setSpriteState(this.node.getChildByName("hotSpr"), 0, true);
        // } else {
        //     Tools.setSpriteState(this.node, 1, true);
        //     Tools.setSpriteState(this.node.getChildByName("newSpr"), 1, true);
        //     Tools.setSpriteState(this.node.getChildByName("hotSpr"), 1, true);
        // }
    }

    click() {
        if (!this.data) {
            UIManager.ins.showToast("敬请期待");
            return;
        }
        LevelManager.ins.curLvInfo = this.data;
        this.gotoLevel();
    }

    gotoLevel() {
        AdManager.hideMoreGame();
        LevelManager.ins.isLiveing = false;
        if (this.unlocked || AdManager.isUnlockAllLevel()) {
            LevelManager.ins.loadLevelPre(this.data);
        } else {
            UIManager.ins.showPop("LockLvUI", BUNDLE_TYPE_ENUM.HOME, false);
        }
    }

    isCurrentLv() {
        let type = LevelManager.ins.curLvInfo.type;
        let lv = LevelManager.ins.curLvInfo.lv;
        if (this.data.type === type && this.data.lv === lv) {
            return true;
        }
        return false;
    }

    clickAddLive() {
        if (this.data) {
            LevelManager.ins.addLiveLv(this.data);
            this.addLiveLv1.active = false;
            this.addLiveLv2.active = true;
            EventManager.ins.emit(EVENT_NAME_ENUM.UPDATE_LIVE_LEVEL_LIST);
        }
    }
}